/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Product;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/28.
 * desc    : 品牌列表适配器
 */
public class BrandAdapter extends AbsAdapter<Product> {

    public BrandAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_product_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final Product data = getItem(position);

        //打开商品详情
        holder.mLlRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OuerDispatcher.presentProductDetailActivity(mContext, data.getId());
            }
        });

        //图片
        OuerApplication.mImageLoader.loadImage(holder.mSdvImage, data.getImage().getUrl());
        //是否新上架
        if(data.isNew()) {
            holder.mIvNew.setVisibility(View.VISIBLE);
        } else {
            holder.mIvNew.setVisibility(View.GONE);
        }
        //标题
        holder.mTvTitle.setText(data.getTitle());
        //价格
        holder.mTvPrice.setText(mContext.getString(R.string.common_price,
                UtilOuer.formatPrice(data.getPrice())));
        //原价
        holder.mTvMarketPrice.setText(mContext.getString(R.string.common_price,
                UtilOuer.formatPrice(data.getMarketPrice())));
        holder.mTvMarketPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );//中划线
        holder.mTvMarketPrice.getPaint().setAntiAlias(true);
        return convertView;
    }


    /**
     * @author zhenshui.xia
     */
    class Holder {
        //
        @Bind(R.id.product_id_root)
        LinearLayout mLlRoot;
        //图片
        @Bind(R.id.product_id_image)
        SimpleDraweeView mSdvImage;
        //是否新上架
        @Bind(R.id.product_id_new)
        ImageView mIvNew;
        //标题
        @Bind(R.id.product_id_title)
        TextView mTvTitle;
        //价格
        @Bind(R.id.product_id_price)
        TextView mTvPrice;
        //原价
        @Bind(R.id.product_id_market_price)
        TextView mTvMarketPrice;

    }
}
